<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="dictOrderType">
        <el-select
          v-model="queryParams.dictOrderType"
          placeholder="请选择订单类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.MARKET_ORDER_TYPE)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="销售类型" prop="dictSaleType">
        <el-select
          v-model="queryParams.dictSaleType"
          placeholder="请选择销售类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.SALE_TYPE)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户简称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户简称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="setSearchParams(queryParams)">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div style="width: 100%; height: 50px; display: flex; justify-content: space-between">
      <div>
        
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['order:marketing-order-info:export']"
          >导出数据
        </el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['order:marketing-order-info:create']"
          >新增
        </el-button>
      </div>
      <div>
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      @register="register"
      @update:currentPage="getTableData"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.pageNo"
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <el-button
          link
          type="danger"
          v-if="row.dictOrderStatus != 20"
          v-hasPermi="['order:marketing-order-info:delete']"
          @click="deleteMarketingOrderInfo(row.id) "
        >
          删除
        </el-button>
        <el-button
          link
          type="primary"
          v-if="row.dictOrderStatus == 10"
          v-hasPermi="['order:marketing-order-info:update']"
          @click="openForm('update', row.id)"
        >
          编辑
        </el-button>
        
        <el-button link type="primary" @click="handleDetail(row.id)"> 查看 </el-button>
      </template>
    </Table>
  </ContentWrap>
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
  <!-- 表单弹窗：添加/修改 -->
  <!-- <MarketingOrderInfoFormVue ref="formRef" @success="getList" /> -->
  <!-- 附件 -->
  <PreveiwArea ref="preveiwArea" />
</template>

<script setup lang="ts" name="MarketingOrderInfo">
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import download from '@/utils/download'
  import * as MarketingOrderInfoApi from '@/api/orderManagement/marketingOrderInfo'
  // import MarketingOrderInfoFormVue from './MarketingOrderInfoForm.vue'
  import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
  const { push } = useRouter()
  const { t } = useI18n() // 国际化
  const message = useMessage() // 消息弹窗
  let tableTitleList = reactive<CrudSchema[]>([]) // 表头的数据
  const loading = ref(true) // 列表的加载中
  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    orderNumber: null,
    dictOrderType: undefined,
    dictSaleType: undefined,
    customerName: null
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中
  // 获得表格的各种操作
  const { tableObject, tableMethods } = useTable({
    getListApi: MarketingOrderInfoApi.getMarketingOrderInfoPage, // 分页接口
    defaultParams: queryParams
  })
  const { getList, setSearchParams } = tableMethods
  const getLabelAll = async () => {
    loading.value = true
    try {
      const data = await MarketingOrderInfoApi.getMarketingOrderInfoPage(queryParams)

      const { allSchemas } = useCrudSchemas(data.labels)
      allSchemas.tableColumns.push({
        align: 'left',
        field: 'action',
        label: '操作',
        width: 150
      })
      tableTitleList = allSchemas.tableColumns
      getList()
    } finally {
      loading.value = false
    }
  }
  const parentRef = ref()
  // 获取table组件
  const register = (parent: any) => {
    parentRef.value = parent
  }
  /**删除 */
  const deleteMarketingOrderInfo = async(id)=> {
    await MarketingOrderInfoApi.deleteMarketingOrderInfo(id)
    message.success(t('common.delSuccess'))
    getList()
  }
  /** 查询列表 */
  const getTableData = async (val) => {
    tableObject.currentPage = val
    getList()
  }
  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    setSearchParams(queryParams)
  }

  /** 添加/修改操作 */
  const openForm = (type: string, id?: number) => {
    push('/orderManagement/orderManagement/marketing/info-edit?type=' + type + '&id=' + id)
  }
  /**查看 */

  const handleDetail = async (id: number) => {
    push('/orderManagement/orderManagement/marketing/info-ddDetail?id=' + id)
  }
  /** 导出按钮操作 */
  const exportRef = ref()
  const handleExport = () => {
    exportRef.value.open()
  }
  //导出数据
  const downloadFun = async () => {
    const val = exportRef.value.params
    let params = {
      fileName: val.name,
      ids: ''
    }
    console.log(val.range)
    if (val.range === 'all') {
      params.ids = ''
    } else {
      parentRef.value.selections.map((item) => {
        params.ids += (item as any).id + ','
      })
    }
    const data = await MarketingOrderInfoApi.exportMarketingOrderInfo(params)
    download.excel(data, val.name)
    parentRef.value.clearTableSelect()
  }
  /**打开表格表头设置**/
  const tableTitleSet = ref()
  const openTableTitleSet = () => {
    const arr = tableTitleList.slice(0, -1)
    let code = 'MarketingOrderInfo'
    tableTitleSet.value.open(arr, code)
  }
  /** 初始化 **/
  const tableHeight = ref(0)
  onMounted(async () => {
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
    await getLabelAll()
    getList()
  })
</script>
<style lang="scss" scoped>
  .btnWrap {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
</style>
